#!/bin/bash

# 查看系统架构
uname -m
# x86_64，则表示系统为amd64；如果输出结果为aarch64，则表示系统为arm64

PROXY=192.168.0.109:10809
curl --proxy $PROXY -LO https://get.helm.sh/helm-v3.13.2-linux-amd64.tar.gz
tar tzvf helm-v3.13.2-linux-amd64.tar.gz
sudo tar Cxzvf /usr/local/bin helm-v3.13.2-linux-amd64.tar.gz --strip-components=1 linux-amd64/helm

helm install cilium cilium/cilium --version 1.14.5 --set image.repository=m.daocloud.io/quay.io/cilium/cilium --namespace kube-system

CILIUM_CLI_VERSION=$(curl -s https://raw.githubusercontent.com/cilium/cilium-cli/main/stable.txt)
CLI_ARCH=amd64
if [ "$(uname -m)" = "aarch64" ]; then CLI_ARCH=arm64; fi
curl --proxy 192.168.0.109:10809 -L --fail --remote-name-all https://github.com/cilium/cilium-cli/releases/download/${CILIUM_CLI_VERSION}/cilium-linux-${CLI_ARCH}.tar.gz{,.sha256sum}
sha256sum --check cilium-linux-${CLI_ARCH}.tar.gz.sha256sum
sudo tar xzvfC cilium-linux-${CLI_ARCH}.tar.gz /usr/local/bin
rm cilium-linux-${CLI_ARCH}.tar.gz{,.sha256sum}

helm upgrade cilium cilium/cilium --version 1.14.5 \
   --namespace kube-system \
   --reuse-values \
   --set hubble.relay.enabled=true \
   --set hubble.ui.enabled=true

HUBBLE_VERSION=$(curl -s https://raw.githubusercontent.com/cilium/hubble/master/stable.txt)
HUBBLE_ARCH=amd64
if [ "$(uname -m)" = "aarch64" ]; then HUBBLE_ARCH=arm64; fi
curl --proxy 192.168.0.109:10809 -L --fail --remote-name-all https://github.com/cilium/hubble/releases/download/$HUBBLE_VERSION/hubble-linux-${HUBBLE_ARCH}.tar.gz{,.sha256sum}
sha256sum --check hubble-linux-${HUBBLE_ARCH}.tar.gz.sha256sum
sudo tar xzvfC hubble-linux-${HUBBLE_ARCH}.tar.gz /usr/local/bin
rm hubble-linux-${HUBBLE_ARCH}.tar.gz{,.sha256sum}

# 安装cilium： https://docs.cilium.io/en/stable/installation/k8s-install-helm/#k8s-install-helm
# Setup Helm repository
helm repo add cilium https://helm.cilium.io/
helm search repo cilium
helm install cilium cilium/cilium --set image.repository=m.daocloud.io/quay.io/cilium/cilium \
--set ipam.mode=kubernetes \
--set k8s.requireIPv4PodCIDR=true \
--set ipv4NativeRoutingCIDR=172.16.0.0/16 \
--set k8sServiceHost=10.96.0.1 \
--set k8sServicePort=443 \
--namespace kube-system

cilium install \
  --version 1.14.5 \
  --set ipam.mode=kubernetes \
  --set k8s.requireIPv4PodCIDR=true \
  --set ipv4NativeRoutingCIDR=172.16.0.0/16

cilium connectivity test
kubectl delete ns cilium-test

kubectl get ciliumnodes -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.ipam.operator-status}{"\n"}{end}'

kubectl create ns cilium-test
kubectl apply -n cilium-test -f
# helm pull cilium/cilium
# kubectl get po -A
# watch kubectl get pods -n calico-system
# helm uninstall cilium --namespace=kube-system

# 安装coreDNS：https://github.com/coredns/helm
helm repo add coredns https://coredns.github.io/helm
# 配置参数：https://github.com/coredns/helm#configuration
helm install coredns coredns/coredns --set service.clusterIP="10.96.0.10" --set image.repository=m.daocloud.io/docker.io/coredns/coredns --namespace kube-system
# helm show values coredns/coredns | grep 'repository'
# To uninstall/delete the coredns deployment: 
# helm uninstall coredns --namespace=kube-system


# 安装metrics-server
helm repo add metrics-server https://kubernetes-sigs.github.io/metrics-server/
helm pull metrics-server/metrics-server

cat <<EOF | tee ~/metrics-server-values.yaml
args:
  - --kubelet-insecure-tls
  - --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-client.crt
  - --requestheader-username-headers=X-Remote-User
  - --requestheader-group-headers=X-Remote-Group
  - --requestheader-extra-headers-prefix=X-Remote-Extra-
extraVolumeMounts:
  - mountPath: /etc/kubernetes/pki
    name: pki
extraVolumes:
  - hostPath:
      path: /etc/kubernetes/pki
    name: pki
# hostNetwork:
#   enabled: true
# containerPort: 4443
EOF

helm install metrics-server /home/vagrant/metrics-server --set image.repository=m.daocloud.io/registry.k8s.io/metrics-server/metrics-server --namespace kube-system -f ~/metrics-server-values.yaml
helm install metrics-server metrics-server/metrics-server --set image.repository=m.daocloud.io/registry.k8s.io/metrics-server/metrics-server --namespace kube-system -f ~/metrics-server-values.yaml
helm uninstall metrics-server --namespace=kube-system
